package com.gitee.loyo.utils;

import java.util.concurrent.atomic.AtomicInteger;

public class SeqHelper {
    private static final AtomicInteger counter = new AtomicInteger(0);
    private static volatile long lastUnixTime;

    /**
     * 4 位自增序列取自时间戳，同一秒内按序列自增长，新秒重计，如0001
     * @param unixTime
     * @return
     */
    public static synchronized String next(long unixTime){
        if (unixTime != lastUnixTime) {
            lastUnixTime = unixTime;
            counter.set(0);
        }
        return String.format("%04d", counter.incrementAndGet() % 9999);
    }
}
